<?php

namespace Library;

use Phalcon\Mvc\User\Component;
use Phalcon\Tag;
use Models\Display;

class Elements extends Component {

    public function paginator($page = null, $form = null) {
//        echo '<ul id ="pages" class="pager"><li>';
//        echo _('Result') . " : " . $page->total_items . " " . _('Display') . " : " . $form->render('HienThi');

        if ($page->current > 1) {
            $previous = "$('#page').val($page->before);$form.submit()";

            $first = "$('#page').val(1);$form.submit()";
            echo Tag::linkTo(array('#myModal', ' <i class="fa fa-step-backward"></i>' . _('First'), 'class' => 'btn',
                'onclick' => $first, 'data-toggle' => 'modal'));

            echo Tag::linkTo(array('#myModal', ' <i class="fa fa-chevron-left"></i>' . _('Previous'), 'class' => 'btn',
                'onclick' => $previous, 'data-toggle' => 'modal'));
        }
        $next = "$('#page').val($page->next);$form.submit()";
        $count = 4 + $page->next;
        /* for ($i=$page->next; $i <$count ; $i++) { 
          if ($i >= $page->total_pages) {  break; }
          $ofset  = "$('#page').val($i);document.adminForm.submit()";

          echo Tag::linkTo(array('#myModal',$i,
          'onclick' =>$ofset,'data-toggle'=>'modal'));
          }
         */
        if ($page->current < $page->total_pages) {
            echo Tag::linkTo(array('#myModal', ' <i class="fa fa-chevron-right"></i>' . _('Next'), 'class' => 'btn',
                'onclick' => $next, 'data-toggle' => 'modal'));
            $last = "$('#page').val($page->last);$form.submit()";

            echo Tag::linkTo(array('#myModal', ' <i class="fa fa-step-forward"></i>' . _('Last'), 'class' => 'btn',
                'onclick' => $last, 'data-toggle' => 'modal'));
        }
        echo '<span>' . _("Pages") . ":" . $page->current . "/" . $page->total_pages . '</span>';

        echo '</ul></li>';
    }

    public function frontPaginator($page = null, $form = null) {
        echo "<select onchange=\"$('#limit').val($(this).val());$form.submit()\">";
        foreach (Display::find() as $value) {
            if ($value->value == $this->view->limit) {
                echo "<option value='$value->value' selected>$value->value</option>";
            } else {
                echo "<option value='$value->value'>$value->value</option>";
            }
        }
        echo "</select>";

        if ($page->current > 1) {
            $previous = "$('#page').val($page->before);$form.submit()";

            $first = "$('#page').val(1);$form.submit()";
            echo Tag::linkTo(array('#myModal', ' <i class="fa fa-step-backward"></i>' . _('First'), 'class' => 'btn',
                'onclick' => $first, 'data-toggle' => 'modal'));

            echo Tag::linkTo(array('#myModal', ' <i class="fa fa-chevron-left"></i>' . _('Previous'), 'class' => 'btn',
                'onclick' => $previous, 'data-toggle' => 'modal'));
        }
        $next = "$('#page').val($page->next);$form.submit()";
        $count = 4 + $page->next;
        /* for ($i=$page->next; $i <$count ; $i++) { 
          if ($i >= $page->total_pages) {  break; }
          $ofset  = "$('#page').val($i);document.adminForm.submit()";

          echo Tag::linkTo(array('#myModal',$i,
          'onclick' =>$ofset,'data-toggle'=>'modal'));
          }
         */
        if ($page->current < $page->total_pages) {
            echo Tag::linkTo(array('#myModal', ' <i class="fa fa-chevron-right"></i>' . _('Next'), 'class' => 'btn',
                'onclick' => $next, 'data-toggle' => 'modal'));
            $last = "$('#page').val($page->last);$form.submit()";

            echo Tag::linkTo(array('#myModal', ' <i class="fa fa-step-forward"></i>' . _('Last'), 'class' => 'btn',
                'onclick' => $last, 'data-toggle' => 'modal'));
        }
        echo '<span>' . _('Pages') . ":" . $page->current . "/" . $page->total_pages . '</span>';

        echo '</ul></li>';
    }

}
